#include<linux/init.h>
#include<linux/module.h>
#include<linux/sched.h>
#include<linux/wait.h>
#include<linux/completion.h>
#include<linux/kthread.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("test@gmail.com");

static struct completion comple;
static struct task_struct* old_task;

int my_test_function(void* args)
{
	printk("enter kernel function!\n");
	printk("the current pid is %d\n",current->pid);
	printk("the value of done of the comple:%d\n",comple.done);
	printk("the state of the init thread is %ld\n",old_task->state);
	
	complete(&comple);

	printk("the value of done of the comple:%d\n",comple.done);
	printk("the state of the init thread is %ld\n",old_task->state);

return 0;
}

int __init complete_init(void)
{
	wait_queue_t data;
	long left_time;

	struct task_struct* new_task = NULL;

	printk("complete init! current->pid = %d\n",current->pid);


	old_task = current;
	
	new_task = kthread_create_on_node(my_test_function,NULL,-1,"comple");


	wake_up_process(new_task);

	init_completion(&comple);

	init_waitqueue_entry(&data, current);


	add_wait_queue(&(comple.wait), &data);

	// add kthread_stop to stop the new_task

//	kthread_stop(new_task);

	
	left_time = schedule_timeout_uninterruptible(1000);

	printk("the pid of new_task is %d\n",new_task->pid);

	printk("the return result of the schedule_timeout_uninterruptible is %ld\n", left_time);

return 0;
}

void __exit complete_exit(void)
{
	printk("complete exit!\n");
return;
}

module_init(complete_init);
module_exit(complete_exit);

